class LinkController < ApplicationController

  before_filter :link_authorize

  def save
    Link.transaction do
      link = Link.create!(:from => Location.adapt(params[:data][:from]),
        :to => Location.adapt(params[:data][:to]),
      :grid => @grid)
    render :text => GridEvent.create_event('itemsLinkedEvent', link, @grid).version
    end
  end

  def destroy
    Link.transaction do
      Link.destroy_at_locations(params[:data][:location1], params[:data][:location2])
      render :text => GridEvent.create_event('itemsUnlinkedEvent', params[:data], @grid).version
    end
  end

  def link_authorize
    head :forbidden if !current_user.can_edit_grid
  end

end
